package com.hsy.linemoney.apiservice;

import com.deparse.dpframe.tools.utils.DPGsonUtil;
import com.deparse.dpframe.tools.utils.DPLogUtil;
import com.hsy.linemoney.R;
import com.hsy.linemoney.requests.RequestTask;
import com.hsy.linemoney.utils.ToastUtil;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;

/**
 * 国家信息相关
 * Created by MartinKent on 2016/9/13.
 */
public class CountryService extends BasicService<JSONArray> {
//    获取国家列表
    public void getCountries(final Callback<CountryListResult> callback, boolean... clearData) {
        if (null != clearData && clearData.length > 0 && clearData[0]) {
            DataHolder.mCountryListResult = null;
        }
        if (null != DataHolder.mCountryListResult) {
            if (null != callback) {
                callback.onSuccess(0, DataHolder.mCountryListResult);
            }
            return;
        }
        String api = "/country/get-countries";
        JSONObject params = ParamsMaker.newInstance().add("Active", "True").get();
        invoke(0, api, params, new Callback<JSONArray>() {
            @Override
            public void onSuccess(int what, JSONArray result) {
                try {
                    DataHolder.mCountryListResult = DPGsonUtil.fromJson("{\"countryList\":" + result.get(0).toString() + "}", CountryListResult.class);
                    if (null != callback) {
                        callback.onSuccess(what, DataHolder.mCountryListResult);
                    }
                } catch (JSONException e) {
                    ToastUtil.showToast(R.string.txt_network_failure);
                    e.printStackTrace();
                    if (null != callback) {
                        callback.onError(what, 0, e.getLocalizedMessage(), e);
                    }
                }
            }

            @Override
            public void onError(int what, int type, String msg, Exception e) {
                if (e instanceof RequestTask.StatusCodeException) {
                    ToastUtil.showToast(msg);
                } else {
                    ToastUtil.showToast(R.string.txt_network_failure);
                }
                if (null != callback) {
                    callback.onError(what, 0, msg, e);
                }
            }
        }, false);
    }

    public static class CountryListResult {
        List<Country> countryList;

        String[] countryNames;

        public List<Country> getCountryList() {
            return countryList;
        }

        public void setCountryList(List<Country> countryList) {
            this.countryList = countryList;
        }

        public String[] getCountryNamesArray() {
            if (null == countryNames) {
                countryNames = new String[getCountryList().size()];
                for (int i = 0; i < countryNames.length; i++) {
                    countryNames[i] = getCountryList().get(i).getName();
                }
            }
            return countryNames;
        }

        public static class Country {
            private String a2;

            private String a3;

            private boolean active;

            private String countryId;

            private String idd;

            private String name;

            private String nameCurrentValue;

            private String number;

            private boolean zipRequired;

            public void setA2(String a2) {
                this.a2 = a2;
            }

            public String getA2() {
                return this.a2;
            }

            public void setA3(String a3) {
                this.a3 = a3;
            }

            public String getA3() {
                return this.a3;
            }

            public void setActive(boolean active) {
                this.active = active;
            }

            public boolean getActive() {
                return this.active;
            }

            public void setCountryId(String countryId) {
                this.countryId = countryId;
            }

            public String getCountryId() {
                return this.countryId;
            }

            public void setIdd(String idd) {
                this.idd = idd;
            }

            public String getIdd() {
                return this.idd;
            }

            public void setName(String name) {
                this.name = name;
            }

            public String getName() {
                return this.name;
            }

            public void setNameCurrentValue(String nameCurrentValue) {
                this.nameCurrentValue = nameCurrentValue;
            }

            public String getNameCurrentValue() {
                return this.nameCurrentValue;
            }

            public void setNumber(String number) {
                this.number = number;
            }

            public String getNumber() {
                return this.number;
            }

            public void setZipRequired(boolean zipRequired) {
                this.zipRequired = zipRequired;
            }

            public boolean getZipRequired() {
                return this.zipRequired;
            }
        }
    }
}
